<?php
function message($status, $msg, $datas) {
  return json_encode([
    "status" => $status,
    "msg" => $msg,
    "datas" => $datas
  ]);
}
function SelectColumn($list) {
  $res = "select ";
  foreach($list as $key => $alias) {
    $res .= " $key as $alias,";
  }
  $res = trim($res);
  return substr($res, 0, strlen($res) - 1);
}
function Assemble($key, $value, $operator) {
  if ($operator === "equal") {
    return " $key = '$value' ";
  }
  if ($operator === "in") {
    return " find_in_set('$value', $key) ";
  } 
  if ($operator === "<") {
    return " $key < '$value' ";
  }
  if ($operator === ">") {
    return " $key > '$value' ";
  }
  die("unavaliable operator.");
}
function Where($condition) {
  $where = " where ";
  foreach($condition as $row) {
    $key = $row[0];
    $value = $row[1];
    $operator = $row[2];
    if (IsEmpty($value)) 
      continue;
    $where .= Assemble($key, $value, $operator). " and ";
  }
  $where = trim($where);
  return substr($where, 0, strlen($where) - 4);
}
function Limit($num) {
  return " limit $num ";
}
function OrderBy($arr) {
  $res = " order by ";
  foreach($arr as $row) {
    $res .= "$row[0] $row[1],";
  }
  $res = trim($res);
  return substr($res, 0, strlen($res) - 1);
}
function From($tab_name) {
  return " from $tab_name ";
}
function IsEmpty($str) {
  return $str === "";
}
function AddWhere($arr, $val) {
  $arr[] = $val;
  return $arr;
}
function getDatas($con, $operator, $last_note_id, $last_note_value, $program_type, $type, $time, $area, $welcome, $num) {
  $select = SelectColumn([
    "id" => "id",
    "pic" => "movie_img_url",
    "name" => "name",
    "url" => "movie_url",
    "play" => "play",
    "addTime" => "addTime",
    "hits" => "hits"
  ]);
  $from = From("ddym_vod");
  $where_arr = [
    [ "yplx", $program_type, "equal" ],
    [ "type", $type, "in" ],
    [ "sy", $time, "equal" ],
    [ "dq", $area, "equal" ]
  ];
  $order_by = OrderBy([
    [ $welcome, "desc" ],
    [ "id", "desc" ]
  ]);
  $limit = Limit($num);
  if ($welcome === "play") {
    $where_arr = AddWhere($where_arr, ["addTime", time() - 60 * 60 * 24 * 30, "<"]);
  } else if ($welcome === "hits") {
    $where_arr = AddWhere($where_arr, [ "hits", 99, ">" ]);
  }

  if (IsEmpty($last_note_value)) {
    $where = Where($where_arr);
    $sql = $select . $from . $where . $order_by . $limit;
    $datas = query($con, $sql);
    return $datas;
  } else {
    $where_arr = AddWhere($where_arr, [ $welcome, $last_note_value, $operator ]);
    if ($operator === "equal") {
      $where_arr = AddWhere($where_arr, [ "id", $last_note_id, "<" ]);
    }
    $where = Where($where_arr);
    $sql = $select . $from . $where . $order_by . $limit;
    $datas = query($con, $sql);
    if ($operator === "<") {
      // 二次查询 直接返回
      return $datas;
    }
    if (count($datas) < $num) {
      $datas = array_merge(
        $datas,
        getDatas($con, "<", $last_note_id, $last_note_value, $program_type, $type, $time, $area, $welcome, $num - count($datas))
      );
      return $datas;
    } else {
      return $datas;
    }
  }
  
}
